package views;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.*;

import db.LogIn;
import db.DBConn;

public class LoginFrame extends JFrame {

    private static final int WIDTH = 275;
    private static final int HEIGHT = 300;
    private static final String TITLE = "CSSA - Log In";
    private JLabel usernameLabel, passwordLabel;
    private JTextField username, password;
    private JButton login, exit;
    private DBConn dbconn;

    public LoginFrame() {
        this.setTitle(TITLE);
        this.setBounds(100, 100, WIDTH, HEIGHT);
        this.setResizable(false);



        KeyListener l = new KeyListener() {

            @Override
            public void keyPressed(KeyEvent arg0) {
                if (arg0.getKeyCode() == KeyEvent.VK_ENTER) {
                    tryLogin();
                } else if (arg0.getKeyCode() == KeyEvent.VK_ESCAPE) {
                    exit();
                }
            }

            @Override
            public void keyTyped(KeyEvent e) {
            }

            @Override
            public void keyReleased(KeyEvent e) {
            }
        };

        JPanel panel = new JPanel();
        panel.setLayout(null);
        LoginFrame.this.getRootPane().setDefaultButton(login);

        Container c = this.getContentPane();

        usernameLabel = new JLabel("User Name");
        usernameLabel.setBounds(25, 25, 200, 25);
        panel.add(usernameLabel);

        username = new JTextField();
        username.setBounds(25, 50, 200, 25);
        username.addKeyListener(l);
        panel.add(username);

        passwordLabel = new JLabel("Password");
        passwordLabel.setBounds(25, 75, 200, 25);
        panel.add(passwordLabel);

        password = new JPasswordField();
        password.setBounds(25, 100, 200, 25);
        password.addKeyListener(l);
        panel.add(password);

        login = new JButton("Log In");
        login.setBounds(25, 180, 100, 30);
        login.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                tryLogin();
            }
        });
        panel.add(login);

        exit = new JButton("Exit");
        exit.setBounds(130, 180, 100, 30);
        exit.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                exit();
            }
        });
        panel.add(exit);

        c.add(panel);

        this.setVisible(true);

    }
    
    public static void main(String[] args) {
        new LoginFrame();
    }

    public void tryLogin() {
        try {

            LogIn.setUSERNAME(username.getText());
            LogIn.setPASSWORD(password.getText());

            dbconn = new DBConn();

            //If connection fails then it won't get past this point

            new mainFrame();

            LoginFrame.this.dispose();

        } catch (Exception e) {
            JOptionPane.showMessageDialog(LoginFrame.this,
                    "Bad Username or Password", "Bad Info", JOptionPane.ERROR_MESSAGE);
            e.printStackTrace();
        }
    }

    public void exit() {
        System.exit(0);
    }
}
